package cn.finder.httpcommons.utils;

import java.util.List;

import cn.finder.httpcommons.exception.ApiException;

public final class RequestValidator {
	 /// <summary>
    /// 错误编码： 参数无效
    /// </summary>
    private final static String ERR_CODE_PARAM_INVALID = "41"; 

    /// <summary>
    /// 错误编码： 参数缺失
    /// </summary>
    private final static String ERR_CODE_PARAM_MISSING = "40";


    /// <summary>
    /// 错误消息： 参数无效
    
    /// </summary>
    private final static String ERR_MSG_PARAM_INVALID = "client-error:Invalid arguments:{0}";

    /// <summary>
    /// 错误消息： 参数缺失
    /// </summary>
    private final static String ERR_MSG_PARAM_MISSING = "client-error:Missing required arguments:{0}";


    /// <summary>
    /// 
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <param name="value">参数值</param>
    /// <param name="maxLength">最大长度</param>
    public static void validateMaxLength(String name, String value, int maxLength)
    {
        if ((value != null) && (value.length() > maxLength))
        {
            throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:"+name);
        }
    }


    /// <summary>
    /// 验证 文件最大长度
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    /// <param name="maxLength"></param>
    public static void validateMaxLength(String name, FileItem value, int maxLength)
    {
        if (((value != null) && (value.getContent() != null)) && (value.getContent().length > maxLength))
        {
            throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:"+ name);
        }
    }


    /// <summary>
    /// 验证 分隔后 数组长度
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value">逗号分隔的字符串</param>
    /// <param name="maxSize"></param>
    public static void validateMaxListSize(String name, String value, int maxSize)
    {
        if (value != null)
        {
            String separator = ",";
            String[] strArray = value.split(separator);
            if ((strArray != null) && (strArray.length > maxSize))
            {
                throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:"+ name);
            }
        }
    }


    /// <summary>
    /// 验证 value的最大值不能超过 maxValue
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    /// <param name="maxValue"></param>
    public static void validateMaxValue(String name, Long value, long maxValue)
    {
        if (value!=null && (value!=null && (value > maxValue)))
        {
            throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:{0}"+name);
        }
    }


    /// <summary>
    /// 验证 value的最小长度 不能小于 minLength
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    /// <param name="minLength"></param>
    public static void validateMinLength(String name, String value, int minLength)
    {
        if ((value != null) && (value.length() < minLength))
        {
            throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:"+name);
        }
    }


    /// <summary>
    /// 验证 value的最小值 不能小于 minValue
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    /// <param name="minValue"></param>
    public static void validateMinValue(String name, Long value, long minValue)
    {
        if (value!=null && (value!=null && (value < minValue)))
        {
            throw new ApiException(ERR_CODE_PARAM_INVALID, "client-error:Invalid arguments:"+name);
        }
    }


    /// <summary>
    /// 验证 字段值不能为空
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    public static void validateRequired(String name, Object value)
    {
        if (value == null)
        {
            throw new ApiException(ERR_MSG_PARAM_INVALID, "client-error:Missing required arguments:"+ name);
        }
        if (value instanceof String)
        {
            String str = (String)value;
            if (str==null ||str.trim().length()==0)
            {
                throw new ApiException(ERR_MSG_PARAM_INVALID, "client-error:Missing required arguments:"+name);
            }
        }

        else if (value instanceof List)
        {
        	List list = (List)value;
            if (list.size()==0)
            {
                throw new ApiException(ERR_MSG_PARAM_INVALID, "client-error:Collection Count is 0 :"+ name);
            }
        }
    }
}
